﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YJKExcelFormTool.DataDefine;

namespace YJKExcelFormTool.Forms
{
	public partial class MaterialTechnicalParametersSettingForm : System.Windows.Forms.Form
	{
		public List<CombinationOfAttributes> CurrentCombinationList
		{
			get
			{
				return this.m_CurrentCombinationList;
			}
		}

		public MaterialTechnicalParametersSettingForm(List<CombinationOfAttributes> allAttributesListOfCategory, List<CombinationOfAttributes> currentCombinationList, string currentColumnName)
		{
			foreach (CombinationOfAttributes combinationOfAttributes in allAttributesListOfCategory)
			{
				if (combinationOfAttributes.AttributesName.Count >= 1)
				{
					this.m_AllCombinationOfAttributesList.Add(combinationOfAttributes);
				}
			}
			this.m_CurrentCombinationList = currentCombinationList;
			this.m_CurrentColumnName = currentColumnName;
			this.InitializeComponent();
			this.InitializeData();
		}

		private void InitializeData()
		{
			for (int i = 0; i < this.m_AllCombinationOfAttributesList.Count; i++)
			{
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				DataGridViewTextBoxCell dataGridViewTextBoxCell = new DataGridViewTextBoxCell();
				string categoryName = this.m_AllCombinationOfAttributesList[i].CategoryName;
				dataGridViewTextBoxCell.Value = categoryName;
				dataGridViewTextBoxCell.ToolTipText = "双击设置统计参数";
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell);
				DataGridViewTextBoxCell dataGridViewTextBoxCell2 = new DataGridViewTextBoxCell();
				string combinationOfAttributesByCategory = this.GetCombinationOfAttributesByCategory(categoryName);
				dataGridViewTextBoxCell2.Value = combinationOfAttributesByCategory;
				dataGridViewTextBoxCell2.ToolTipText = "双击设置统计参数";
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell2);
				this.dbg.Rows.Add(dataGridViewRow);
			}
		}

		private string GetCombinationOfAttributesByCategory(string categoryName)
		{
			string text = "";
			int num = this.m_CurrentCombinationList.FindIndex((CombinationOfAttributes s) => s.CategoryName.CompareTo(categoryName) == 0);
			if (num != -1)
			{
				CombinationOfAttributes combinationOfAttributes = this.m_CurrentCombinationList[num];
				for (int i = 0; i < combinationOfAttributes.AttributesName.Count; i++)
				{
					string text2 = combinationOfAttributes.AttributesName[i];
					if (i == combinationOfAttributes.AttributesName.Count - 1)
					{
						text += combinationOfAttributes.AttributesName[i];
					}
					else
					{
						text = text + combinationOfAttributes.AttributesName[i] + combinationOfAttributes.Delimiter[i];
					}
				}
			}
			return text;
		}

		private void dbg_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			DataGridView dataGridView = (DataGridView)sender;
			CombinationOfAttributes allAttributesOfCategory = this.m_AllCombinationOfAttributesList[dataGridView.CurrentCell.RowIndex];
			CombinationOfAttributes currentCombination = null;
			if (this.m_CurrentCombinationList.Count > 0)
			{
				string categortyName = dataGridView.CurrentRow.Cells[0].Value.ToString();
				int num = this.m_CurrentCombinationList.FindIndex((CombinationOfAttributes s) => s.CategoryName.CompareTo(categortyName) == 0);
				if (num != -1)
				{
					currentCombination = this.m_CurrentCombinationList[num];
				}
			}
			using (CombinationOfAttributesForm combinationOfAttributesForm = new CombinationOfAttributesForm(allAttributesOfCategory, currentCombination))
			{
				string categoryName = dataGridView.CurrentRow.Cells[0].Value.ToString();
				combinationOfAttributesForm.Text = string.Concat(new string[]
				{
					"“",
					categoryName,
					"-",
					this.m_CurrentColumnName,
					"”",
					combinationOfAttributesForm.Text
				});
				if (combinationOfAttributesForm.ShowDialog(this) == DialogResult.OK)
				{
					List<string> list = new List<string>();
					List<string> delimiter = new List<string>();
					combinationOfAttributesForm.GetCombinationOfAttributes(ref list, ref delimiter);
					int num2 = this.m_CurrentCombinationList.FindIndex((CombinationOfAttributes s) => s.CategoryName.CompareTo(categoryName) == 0);
					if (num2 == -1)
					{
						CombinationOfAttributes item = new CombinationOfAttributes(categoryName, list, delimiter);
						this.m_CurrentCombinationList.Add(item);
					}
					else
					{
						this.m_CurrentCombinationList[num2].SetAttributes2ndDelimiter(list, delimiter);
					}
					string combinationOfAttributesByCategory = this.GetCombinationOfAttributesByCategory(categoryName);
					dataGridView.CurrentRow.Cells[1].Value = combinationOfAttributesByCategory;
				}
			}
		}

		private List<CombinationOfAttributes> m_AllCombinationOfAttributesList = new List<CombinationOfAttributes>();

		private List<CombinationOfAttributes> m_CurrentCombinationList = new List<CombinationOfAttributes>();

		private string m_CurrentColumnName = "";
	}
}
